// pages/fabu/fabu.js
var config = require('../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day: [1, 2, 3, 4, 5, 6, 7],
    index: 0,

    imgs: [],
    uploadImgs:'',
    //是否同意协议，电话，公告内容
    ty: false,
    phone: '',
    content: ''
  },
  onLoad(options) {
    this.data.categoryId = options.cid
    let phone = wx.getStorageSync('phone')
    if(phone==null || phone=='') {
      // request手机号
      wx.request({
        url: config.HOST + '/user/getUserById',
        data: {
          token: wx.getStorageSync("token")
        },
        success: (res) => {
          console.log(res)
          this.setData({
            phone: res.data.phone
          })
          wx.setStorageSync("phone", res.data.phone)
        }
      })
    }
    
    this.setData({
      phone
    })
          

  },
  chooseImg() {
    let _this = this;
    wx.chooseImage({
      count: 6,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        const tempFilePaths = res.tempFilePaths
        _this.setData({
          imgs: tempFilePaths
        })
  // 上传文件(没有做删除的。。。。。。。。。。。。。。。。。。。。)
        tempFilePaths.forEach((item,index)=>{
          console.log(item)
          wx.uploadFile({
            url: config.IMGHOST + '/img/upload', //仅为示例，非真实的接口地址
            filePath: item,
            name: 'imgs',
            success(res) {
              const data = res.data
              console.log(data)
              _this.data.uploadImgs += data+","
            }
          })
        })
      }
    })
  },
  //置顶天数选择
  bindPickerChange: function(e) {
    this.setData({
      index: e.detail.value
    })
  },
  // 获取公告内容，手机号，同意协议
  bindContent(e) {
    this.data.content = e.detail.value
    console.log(this.data.content)
  },
  bindPhone(e) {
    this.data.phone = e.detail.value
  },
  radioChange(e) {
    console.log(e.detail)
    this.data.ty = e.detail.value
  },
  // 发布
  release() {
    let _this = this
    let token = wx.getStorageSync('token')
    let conetnt = this.data.content
    let phone = this.data.phone
    let ty = this.data.ty
    if (conetnt==null||conetnt.trim()=='') {
      wx.showToast({
        title: '请填写公告信息',
        icon : 'none'
      })
      return;
    }
    if (phone == null || phone.trim() == '') {
      wx.showToast({
        title: '请填写手机号码',
        icon: 'none'
      })
      return;
    }
    if (ty==false) {
      wx.showToast({
        title: '请同意免责说明',
        icon: 'none'
      })
      return;
    }
    var myreg = /^[1][3,4,5,7,8][0-9]{9}$/;
    if (!myreg.test(phone)) {
      wx.showToast({
        title: '请输入正确的手机号',
        icon: 'none'
      })
      return;
    }
    wx.showLoading({
      title: '发布中',
    })
    wx.request({
      url: config.HOST +'/notice/release',
      data: {
        token,
        categoryId: this.data.categoryId,
        img: this.data.uploadImgs.substring(0, this.data.uploadImgs.length-1),
        content: conetnt,
        phone
      },
      success(res){
        let data = res.data
        console.log(data)
        if(data.status==200) {
          if(data.data==1) {
            wx.hideLoading()
            wx.showToast({
              title: '发布成功',
            })
            _this.setData({
              imgs:[],
              uploadImgs:'',
              conetnt:[]
            })
          }
        }
      }
    })
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (res) {
    return {
      title: '公众墙',
      path: '/pages/index/index',
      // imageUrl: '这个是显示的图片，不写就默认当前页面的截图',
      success: function (shareTickets) {
        console.info(shareTickets + '成功');
        // 转发成功
      },
      fail: function (res) {
        console.log(res + '失败');
        // 转发失败
      },
      complete: function (res) {
        // 不管成功失败都会执行
      }
    }
  }
})